import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
# 加载数据集
data = pd.read_csv('iris.csv')
# 删除有缺失值的行
data.dropna(inplace=True)
# 删除明显错误数据的行
data = data[(data >= 0).all(axis=1)]
# 求出每种鸢尾花萼片长度的平均值、中位数和标准差
sepal_length_values = data[data.columns[:-1]].values
sepal_length_means = np.mean(sepal_length_values, axis=0)
sepal_length_medians = np.median(sepal_length_values, axis=0)
sepal_length_std = np.std(sepal_length_values, axis=0)
# 归一化数据
scaler = StandardScaler()
data_normalized = scaler.fit_transform(data[data.columns[:-1]])
data_normalized = pd.DataFrame(data_normalized, columns=data.columns[:-1])
# 将数据清洗及归一化后的结果保存为新的csv文件
data_with_class = pd.concat([data_normalized, data[['Class']]], axis=1)
data_with_class.to_csv('iris_cleaned_normalized.csv', index=False)
# 打印结果
print(f"每种鸢尾花萼片长度的平均值：\n{sepal_length_means}")
print(f"每种鸢尾花萼片长度的中位数：\n{sepal_length_medians}")
print(f"每种鸢尾花萼片长度的标准差：\n{sepal_length_std}")